/**
 * @Author yinyunqi
 * @date 2018年2月1日
 * @Content 多个realm认证策略
 */
package com.yongqi.shiro;

import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

public class DefaultModularRealm extends ModularRealmAuthenticator{

    /**
     * 存放realm
     */
    private Map<String, Object> definedRealms;
    
    public void setDefinedRealms(Map<String, Object> definedRealms) {
	this.definedRealms = definedRealms;
    }
    /**
     * 根据用户类型判断使用哪个realm
     */
    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
	super.assertRealmsConfigured();
	Realm realm = null;
	UsernamePasswordUsertypeToken token = (UsernamePasswordUsertypeToken) authenticationToken;
	if ("back".equals(token.getUserType())) {
	    realm = (Realm) this.definedRealms.get("backShiroRealm");
	}else if ("front".equals(token.getUserType())) {
	    realm = (Realm) this.definedRealms.get("myShiroRealm");
	}
	return this.doSingleRealmAuthentication(realm, authenticationToken);
    }
}









